package a2;

import java.util.ArrayList;
import java.util.List;
import org.openrdf.repository.*;
import org.openrdf.repository.sparql.SPARQLRepository;
import org.openrdf.model.Value;
import org.openrdf.query.*;

public class uppg32 {

	public static List<String> query(String select, String query){
		// http://vm15.cs.lth.se:8085/openrdf-sesame/repositories/Assignment2
		Repository myRepository = new SPARQLRepository("http://vm15.cs.lth.se:8085/openrdf-sesame/repositories/KnowRob");
		RepositoryConnection con;
		List<String> resultStrings = new ArrayList<String>();
		String prefix = "PREFIX :<http://ias.cs.tum.edu/kb/knowrob.owl#> " + 
				"PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> " + 
				"PREFIX computable:<http://ias.cs.tum.edu/kb/computable.owl#> " + 
				"PREFIX owl2xml:<http://www.w3.org/2006/12/owl2-xml#> " + 
				"PREFIX xsd:<http://www.w3.org/2001/XMLSchema#> " + 
				"PREFIX owl:<http://www.w3.org/2002/07/owl#> " + 
				"PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + 
				"PREFIX knowrob:<http://ias.cs.tum.edu/kb/knowrob.owl#>";

		try {
			myRepository.initialize();
			con = myRepository.getConnection();
			try {
				//Viktig!!
				String queryString = prefix + "SELECT " + select + " WHERE { " + query + " }";
				TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
				TupleQueryResult result = tupleQuery.evaluate();
				List<String> bindingNames = result.getBindingNames();
				
				// Add the variable names to be printed with the results
				String variables = "";
				for(String s : bindingNames){
					// Pad the string with whitespaces for better printout  (when +2 columns)
					for(int i = 30 - s.length(); i > 0; --i){
						s += " ";
					}
					variables += s + " | ";
				}
				resultStrings.add(variables);
				
				// Add the results to the return list
				while (result.hasNext()) {
					BindingSet bindingSet = result.next();
					String thisResult = "";
					for(int i = 0; i < bindingNames.size(); ++i){
						String value = "";
						Value v = bindingSet.getValue(bindingNames.get(i));
						value = v.stringValue();
						int cutIndex = value.indexOf('#');
						value = value.substring(cutIndex+1);
						// Pad the string with whitespaces for better printout  (when +2 columns)
						for(int j = 30 - value.length(); j > 0; --j){
							value += " ";
						}
						thisResult += value + " | ";
					}
					resultStrings.add(thisResult);
				}
				result.close();
			}finally {
				con.close();
				myRepository.shutDown();
			}
		}catch(Exception e){
			System.out.println("Something went wrong.");
		}	
		return resultStrings;
	}

	public static void main(String[] args) {
		String select = "?putAction ?subAction";
		String s1 = "?putAction rdfs:subClassOf knowrob:Movement-TranslationEvent .";
		String s2 = "FILTER regex(str(?putAction), \"put\", \"i\")";
		String s3 = "?putAction rdfs:subClassOf ?a .";
		String s4 = "?a owl:onProperty knowrob:subAction .";
		String s5 = "?a owl:someValuesFrom ?subAction .";
		List<String> result = query(select, s1+s2+s3+s4+s5);
		System.out.println("||| RESULT |||");
		for(String s : result){
			System.out.println(s);
		}
		System.exit(0); // For some reason the program does not terminate automatically here
	}

}
